﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EMath.Controls.Geometry {
	/// <summary>
	/// Interaction logic for Planimetrics.xaml
	/// </summary>
	public partial class Planimetrics : UserControl {
		public Planimetrics( ) {
			InitializeComponent( );
		}

		private void Back(object sender, RoutedEventArgs e) {
			if (this.Parent is UIElement)
				(this.Parent as UIElement).Visibility = System.Windows.Visibility.Hidden;
		}

		// ZMIENNE
		public EMath.Objects.PopUp MyPopUp = new EMath.Objects.PopUp( );
		public EMath.Objects.Geometry MyGeometry = new EMath.Objects.Geometry( );
		public double ZQ;

		// Zadarzenia
		private void GeometryControl_Load(object sender, EventArgs e) {
			this.La_Title.Text = "Geometria - Planometria";
			this.La_Calc.Text = "Oblicz: ";
			this.GB_Calc.Header = "Rozwiązanie:";
			this.GB_Image.Header = "Rysunek pomocniczy:";
			this.GB_Vars.Header = "Dane:";
			this.Bu_Calc.Content = "Wykonaj obliczenia";
			this.CB_var1.Items.Add("obwód");
			this.CB_var1.Items.Add("pole");
			if (this.CB_var1.Items.Count > 0) {
				this.CB_var1.SelectedIndex = 0;
				this.CB_var1.Visibility = Visibility.Visible;
			}
			if (this.CB_var2.Items.Count > 0) {
				this.CB_var2.SelectedIndex = 0;
				this.CB_var2.Visibility = Visibility.Visible;
			}
			if (this.CB_var3.Items.Count > 0) {
				this.CB_var3.SelectedIndex = 0;
				this.CB_var3.Visibility = Visibility.Visible;
			}
			//MyGeometry.Rese();
		}

		private void CB_var1_SelectedIndexChanged(object sender, SelectionChangedEventArgs e) {
			this.Bu_Calc.IsEnabled = false;
			this.WP_Vars.Visibility = Visibility.Collapsed;
			this.FClear( );
			this.CB_var3.IsEnabled = false;
			this.CB_var2.Items.Clear( );
			this.CB_var2.Items.Add("trójkąta");
			this.CB_var2.Items.Add("trapezu");
			this.CB_var2.Items.Add("równoległoboku");
			this.CB_var2.Items.Add("prostokąta");
			this.CB_var2.Items.Add("rombu");
			this.CB_var2.Items.Add("kwadratu");
			this.CB_var2.Items.Add("deltoidu");
			this.CB_var2.Items.Add("elipsy");
			this.CB_var2.Items.Add("koła");
			this.CB_var2.SelectedIndex = 0;
			this.CB_var2.IsEnabled = true;
			MyGeometry.ResetDANE( );
		}
		private void CB_var2_SelectedIndexChanged(object sender, SelectionChangedEventArgs e) {
			this.Bu_Calc.IsEnabled = false;
			this.WP_Vars.Visibility = Visibility.Collapsed;
			this.CB_var3.Items.Clear( );
			if (this.CB_var2.SelectedIndex == 0) {
				this.CB_var3.Items.Add("zwykłego");
				this.CB_var3.Items.Add("równoramiennego");
				this.CB_var3.Items.Add("równobocznego");
			}
			if (this.CB_var3.Items.Count > 0) {
				this.CB_var3.SelectedIndex = 0;
				this.CB_var3.IsEnabled = true;
			} else {
				this.CB_var3.IsEnabled = false;
				this.FShow(CB_var1.SelectedIndex, CB_var2.SelectedIndex, CB_var3.SelectedIndex);
				this.WP_Vars.Visibility = Visibility.Visible;
			}
			MyGeometry.ResetDANE( );
		}
		private void CB_var3_SelectedIndexChanged(object sender, SelectionChangedEventArgs e) {
			int ix = (2 - this.CB_var1.SelectedIndex) * 9;
			this.Bu_Calc.IsEnabled = false;
			if (this.CB_var2.SelectedIndex == 0 + ix && this.CB_var3.SelectedIndex == 3) {
				this.CB_var2.SelectedIndex = 1 + ix;
			} else if (this.CB_var2.SelectedIndex == 4 + ix && this.CB_var3.SelectedIndex == 8) {
				this.CB_var2.SelectedIndex = 5 + ix;
			} else {
				this.FShow(CB_var1.SelectedIndex, CB_var2.SelectedIndex, CB_var3.SelectedIndex);
				this.WP_Vars.Visibility = Visibility.Visible;
			}
			MyGeometry.ResetDANE( );
		}
		private void TB_V_N_TextChanged(object sender, TextChangedEventArgs e) {
			if (sender is TextBox)
				if ((sender as TextBox).Parent is Grid)
					if (((sender as TextBox).Parent as Grid).IsVisible)
						this.FCheck((int)this.CB_var1.SelectedIndex, (int)this.CB_var2.SelectedIndex, (int)this.CB_var3.SelectedIndex, sender);
		}
		private void Bu_Calc_Click(object sender, EventArgs e) {
			FCalc(this.CB_var1.SelectedIndex, this.CB_var2.SelectedIndex, this.CB_var3.SelectedIndex);
		}
		private void PB_Image_Click(object sender, MouseButtonEventArgs e) {
			FImage(this.CB_var1.SelectedIndex, this.CB_var2.SelectedIndex, this.CB_var3.SelectedIndex);
		}

		// Wyświetlenia pól
		private void FShow(int Types, int Fig, int FigS) {
			this.WP_Vars.Visibility = Visibility.Collapsed;
			FClear( );
			if (Types > 1) {
				Fig = Fig + 9;
			}
			switch (Fig) {
				case 0:
					FShowSet(Values: MyGeometry.F_Tro_Load(Types, FigS));
					break;
				case 1:
					FShowSet(Values: MyGeometry.F_Tra_Load(Types));
					break;
				case 2:
					FShowSet(Values: MyGeometry.F_Row_Load(Types));
					break;
				case 3:
					FShowSet(Values: MyGeometry.F_Pro_Load(Types));
					break;
				case 4:
					FShowSet(Values: MyGeometry.F_Rom_Load(Types));
					break;
				case 5:
					FShowSet(Values: MyGeometry.F_Kwa_Load(Types));
					break;
				case 6:
					FShowSet(Values: MyGeometry.F_Del_Load(Types));
					break;
				case 7:
					FShowSet(Values: MyGeometry.F_Eli_Load(Types));
					break;
				case 8:
					FShowSet(Values: MyGeometry.F_Kol_Load(Types));
					break;
			}
			FImageSet(Types, Fig, FigS);
			//CCErr.Clear( );
		}
		private void FShowSet(string[ ] Values) {
			if (Values[0] != null) {
				this.G_V_1.Visibility = Visibility.Visible;
				this.La_V_1.Text = Values[0];
			}
			if (Values[1] != null) {
				this.G_V_2.Visibility = Visibility.Visible;
				this.La_V_2.Text = Values[1];
			}
			if (Values[2] != null) {
				this.G_V_3.Visibility = Visibility.Visible;
				this.La_V_3.Text = Values[2];
			}
			if (Values[3] != null) {
				this.G_V_4.Visibility = Visibility.Visible;
				this.La_V_4.Text = Values[3];
			}
			if (Values[4] != null) {
				this.G_V_5.Visibility = Visibility.Visible;
				this.La_V_5.Text = Values[4];
			}
			if (Values[5] != null) {
				this.G_V_6.Visibility = Visibility.Visible;
				this.La_V_6.Text = Values[5];
			}
			if (Values[6] != null) {
				this.G_V_7.Visibility = Visibility.Visible;
				this.La_V_7.Text = Values[6];
			}
			if (Values[7] != null) {
				this.G_V_8.Visibility = Visibility.Visible;
				this.La_V_8.Text = Values[7];
			}
		}
		private void FImage(int Types, int Fig, int FigS) {
			try {
				MyPopUp = new EMath.Objects.PopUp( );
				MyPopUp.Width = 300;
				MyPopUp.Height = 300;
				FImageSet(Types, Fig, FigS);
				//MyPopUp.Top = (this.Parent as Window).Top + GB_Image.Location.X + PB_Image.Location.X - 63;
				//MyPopUp.Left = (this.Parent as Window).Left + GB_Image.Location.Y + PB_Image.Location.Y + 65;
				MyPopUp.Show( );
			} catch (Exception) { }
		}
		private void FImageSet(int Types, int Fig, int FigS) {
			try {
				switch (Fig) {
					case 0:
						PB_Image.Source = MyGeometry.F_Tro_ImageX(true, Types, FigS);
						MyPopUp.SetImage(MyGeometry.F_Tro_ImageX(false, Types, FigS));
						break;
					case 1:
						PB_Image.Source = MyGeometry.F_Tra_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Tra_ImageX(false, Types));
						break;
					case 2:
						PB_Image.Source = MyGeometry.F_Row_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Row_ImageX(false, Types));
						break;
					case 3:
						PB_Image.Source = MyGeometry.F_Pro_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Pro_ImageX(false, Types));
						break;
					case 4:
						PB_Image.Source = MyGeometry.F_Rom_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Rom_ImageX(false, Types));
						break;
					case 5:
						PB_Image.Source = MyGeometry.F_Kwa_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Kwa_ImageX(false, Types));
						break;
					case 6:
						PB_Image.Source = MyGeometry.F_Del_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Del_ImageX(false, Types));
						break;
					case 7:
						PB_Image.Source = MyGeometry.F_Eli_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Eli_ImageX(false, Types));
						break;
					case 8:
						PB_Image.Source = MyGeometry.F_Kol_ImageX(true, Types);
						MyPopUp.SetImage(MyGeometry.F_Kol_ImageX(false, Types));
						break;
				}
			} catch (Exception) { }
		}

		// Sprawdzenie danych
		private void FDANESet( ) {
			MyGeometry.ResetDANE( );
			if (double.TryParse(this.TB_V_1.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[0] = ZQ;
			}
			if (double.TryParse(this.TB_V_2.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[1] = ZQ;
			}
			if (double.TryParse(this.TB_V_3.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[2] = ZQ;
			}
			if (double.TryParse(this.TB_V_4.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[3] = ZQ;
			}
			if (double.TryParse(this.TB_V_5.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[4] = ZQ;
			}
			if (double.TryParse(this.TB_V_6.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[5] = ZQ;
			}
			if (double.TryParse(this.TB_V_7.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[6] = ZQ;
			}
			if (double.TryParse(this.TB_V_8.Text.Replace(".", ","), out ZQ)) {
				MyGeometry.DANE[7] = ZQ;
			}
		}
		private void FCheck(int Types, int Fig, int FigS, object OBJ) {
			/*Boolean j = true;
			FDANESet( );
			Control CONO = (Control)OBJ;
			if (CONO.Text.Replace(".", ",") == "") {
				CCErr.SetError(CONO, "Wymagane!");
			} else {
				if (double.TryParse(CONO.Text.Replace(".", ","), out ZQ) == false) {
					CCErr.SetError(CONO, "To nie jest liczba!");
				} else {
					if (ZQ <= 0) {
						CCErr.SetError(CONO, "Wymagana liczba naturalna!");
					} else {
						CCErr.SetError(CONO, "");
					}
				}
			}
			if (Types > 1) {
				Fig = Fig + 9;
			}
			if (this.G_V_1.IsVisible) {
				if (CCErr.GetError(this.TB_V_1) != "" || MyGeometry.DANE[0] == 0) {
					j = false;
				}
			}
			if (this.G_V_2.IsVisible) {
				if (CCErr.GetError(this.TB_V_2) != "" || MyGeometry.DANE[1] == 0) {
					j = false;
				}
			}
			if (this.G_V_3.IsVisible) {
				if (CCErr.GetError(this.TB_V_3) != "" || MyGeometry.DANE[2] == 0) {
					j = false;
				}
			}
			if (this.G_V_4.IsVisible) {
				if (CCErr.GetError(this.TB_V_4) != "" || MyGeometry.DANE[3] == 0) {
					j = false;
				}
			}
			if (this.G_V_5.IsVisible) {
				if (CCErr.GetError(this.TB_V_5) != "" || MyGeometry.DANE[4] == 0) {
					j = false;
				}
			}
			if (this.G_V_6.IsVisible) {
				if (CCErr.GetError(this.TB_V_6) != "" || MyGeometry.DANE[5] == 0) {
					j = false;
				}
			}
			if (this.G_V_7.IsVisible) {
				if (CCErr.GetError(this.TB_V_7) != "" || MyGeometry.DANE[6] == 0) {
					j = false;
				}
			}
			if (this.G_V_8.IsVisible) {
				if (CCErr.GetError(this.TB_V_8) != "" || MyGeometry.DANE[7] == 0) {
					j = false;
				}
			}
			if (j == true) {
				CCErr.SetError(this.Bu_Calc, "");
				switch (Fig) {
					case 0:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Tro_Check(Types, FigS));
						break;
					case 1:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Tra_Check(Types));
						break;
					case 2:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Row_Check(Types));
						break;
					case 3:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Pro_Check(Types));
						break;
					case 4:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Rom_Check(Types));
						break;
					case 5:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Kwa_Check(Types));
						break;
					case 6:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Del_Check(Types));
						break;
					case 7:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Eli_Check(Types));
						break;
					case 8:
						CCErr.SetError(this.Bu_Calc, MyGeometry.F_Kol_Check(Types));
						break;
				}
				if (CCErr.GetError(this.Bu_Calc) != "") {
					j = false;
				}
			}
			this.Bu_Calc.IsEnabled = j;
			 * */
		}

		// Obliczenie
		private void FCalc(int Types, int Fig, int FigS) {
			//this.RTB_Calc.Clear( );
			string[ ] DATA = { "", "" };
			if (Types > 1) {
				Fig = Fig + 9;
			}
			switch (Fig) {
				case 0:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Tro_Ob(FigS);
							break;
						case 1:
							DATA = MyGeometry.F_Tro_P( );
							break;
					}
					break;
				case 1:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Tra_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Tra_P( );
							break;
					}
					break;
				case 2:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Row_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Row_P( );
							break;
					}
					break;
				case 3:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Pro_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Pro_P( );
							break;
					}
					break;
				case 4:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Rom_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Rom_P( );
							break;
					}
					break;
				case 5:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Kwa_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Kwa_P( );
							break;
					}
					break;
				case 6:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Del_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Del_P( );
							break;
					}
					break;
				case 7:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Eli_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Eli_P( );
							break;
					}
					break;
				case 8:
					switch (Types) {
						case 0:
							DATA = MyGeometry.F_Kol_Ob( );
							break;
						case 1:
							DATA = MyGeometry.F_Kol_P( );
							break;
					}
					break;
			}
			if (Properties.Settings.Default.CalcBox) {
				this.RTB_Calc.AppendText(DATA[1]);
			} else {
				this.RTB_Calc.AppendText(DATA[0]);
			}
		}

		// Wyczyszczenie pól
		private void FClear( ) {
			//this.RTB_Calc.Clear( );
			this.G_Image.Background = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/Resources/ImagesG/GI_BG.jpg", UriKind.Absolute)));
			//this.PB_Image.Refresh( );
			this.G_V_1.Visibility = Visibility.Collapsed;
			this.La_V_1.Text = "";
			this.TB_V_1.Clear( );
			this.G_V_2.Visibility = Visibility.Collapsed;
			this.La_V_2.Text = "";
			this.TB_V_2.Clear( );
			this.G_V_3.Visibility = Visibility.Collapsed;
			this.La_V_3.Text = "";
			this.TB_V_3.Clear( );
			this.G_V_4.Visibility = Visibility.Collapsed;
			this.La_V_4.Text = "";
			this.TB_V_4.Clear( );
			this.G_V_5.Visibility = Visibility.Collapsed;
			this.La_V_5.Text = "";
			this.TB_V_5.Clear( );
			this.G_V_6.Visibility = Visibility.Collapsed;
			this.La_V_6.Text = "";
			this.TB_V_6.Clear( );
			this.G_V_7.Visibility = Visibility.Collapsed;
			this.La_V_7.Text = "";
			this.TB_V_7.Clear( );
			this.G_V_8.Visibility = Visibility.Collapsed;
			this.La_V_8.Text = "";
			this.TB_V_8.Clear( );
		}
	}
}